//
//  CustomerInfoTwoVC.m
//  AI
//
//  Created by zjhc on 2018/2/14.
//  Copyright © 2018年 zjhc. All rights reserved.
//

#import "CustomerInfoTwoVC.h"
#import "UIColor+Extension.h"
#import "WTAudioObject.h"
#import "AudioFileModel.h"
#import "DataBase.h"
#import <SVProgressHUD.h>

@interface CustomerInfoTwoVC ()
{
    NSString *_pathUrl;
}

@property (weak, nonatomic) IBOutlet UIButton *avatar;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIButton *idCardPhoto;
- (IBAction)receptionAction:(id)sender;

-(IBAction)receptionDoneAction:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *recordTimeLabel;
@property (weak, nonatomic) IBOutlet UIButton *receptionButton;
@property (weak, nonatomic) IBOutlet UILabel *noCustomerInfo;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int count;


@end

@implementation CustomerInfoTwoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"客户信息";
    [self.avatar  setBackgroundImage:[UIImage imageNamed:@"头像测试"] forState:UIControlStateNormal];
    self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2;
    self.avatar.layer.masksToBounds = YES;
    self.name.text = @"王璐丹";
    [self.idCardPhoto  setBackgroundImage:[UIImage imageNamed:@"timg"] forState:UIControlStateNormal];
    self.recordTimeLabel.hidden = YES;

    // Do any additional setup after loading the view.
}

#pragma mark - 音频上传服务
-(void)audioCommit:(AudioFileModel *) audioFileModel{
    [WTHttpRequest requestMultipartFileJsonPostWithUrlStr:@"audiosrv/audio/upload" params:@{@"access_token":[NSUserDefaultsUtil gettoken],@"path":_pathUrl} onComplete:^(NSError *error, NSDictionary *resultJson) {
        NSArray *array = [_pathUrl componentsSeparatedByString:@"_"];
        
        if(!error){
            
            if (audioFileModel == nil) {
                AudioFileModel *audioFileModel =[[AudioFileModel alloc]init];
                audioFileModel.audioFileName = array[1];
                audioFileModel.isSuccess = YES;
                audioFileModel.date = [NSDate date];
                [[DataBase sharedDataBase] addAudioFile:audioFileModel];
            } else {
                audioFileModel.audioFileName = array[1];
                audioFileModel.isSuccess = YES;
                audioFileModel.date = [NSDate date];
                [[DataBase sharedDataBase] updateAudioFile:audioFileModel];
            }
            
            [SVProgressHUD showSuccessWithStatus:@"上传成功"];
        }else
        {
            
            if (audioFileModel == nil) {
                AudioFileModel *audioFileModel =[[AudioFileModel alloc]init];
                audioFileModel.audioFileName = array[1];
                audioFileModel.isSuccess = NO;
                audioFileModel.date = [NSDate date];
                [[DataBase sharedDataBase] addAudioFile:audioFileModel];
            } else {
                audioFileModel.audioFileName = array[1];
                audioFileModel.isSuccess = NO;
                audioFileModel.date = [NSDate date];
                [[DataBase sharedDataBase] updateAudioFile:audioFileModel];
            }
            
            [SVProgressHUD showErrorWithStatus:error.domain];
        
        }
    }];
}

#pragma mark - 开始播放音频
-(void)startAudio{
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    NSString * urlstr = [NSString stringWithFormat:@"%@_%llu",[NSUserDefaultsUtil getUserPhone],recordTime];
    _pathUrl = urlstr;
    [[WTAudioObject shareInstance] startAudioWith:_pathUrl];;
   
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.count = 0;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(repeatShowTime:) userInfo:nil repeats:YES];
}

#pragma mark - 暂停播放
-(void)pauseAudio {
    [self.timer setFireDate:[NSDate distantFuture]];
    [[WTAudioObject shareInstance] stopAudio];
    //录音文件不上传
}

#pragma mark - 继续播放
-(void)resumeRecordAudio {
    [self.timer setFireDate:[NSDate date]];
    [[WTAudioObject shareInstance] resumeRecordAudio];
}
#pragma mark - 停止播放
-(void)stopAudio{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.count = 0;
    self.recordTimeLabel.text = @"00:00:00";
    
    [[WTAudioObject shareInstance] stopAudio];
    //录音文件上传
    [self audioCommit:nil];
    //    [[WTAudioObject shareInstance] playAudioWithPath:_pathUrl];
}

#pragma mark - 重复显示时间
- (void)repeatShowTime:(NSTimer *)tempTimer {
    
    self.count++;
    
    self.recordTimeLabel.text = [NSString stringWithFormat:@"00:%02d:%02d",self.count/60,self.count%60];
}

#pragma - 销毁NSTimer
- (void)dealloc {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - 接待事件
- (IBAction)receptionAction:(id)sender {
        self.receptionButton.selected = !self.receptionButton.selected;
    
        if (self.receptionButton.selected) {
            
            if ([self.recordTimeLabel.text isEqualToString:@"00:00:00"]||self.recordTimeLabel.text ==nil) {
                [self startAudio];
            } else {
                [self resumeRecordAudio];
            }
            
            self.recordTimeLabel.hidden = NO;
             [self.receptionButton setTitle:@"暂停接待" forState:UIControlStateNormal];
            [self.receptionButton setBackgroundColor:[UIColor colorWithHexString:@"E86374"]];
        } else {
            [self pauseAudio];
             [self.receptionButton setTitle:@"接待" forState:UIControlStateNormal];
            [self.receptionButton setBackgroundColor:[UIColor colorWithHexString:@"455DAA"]];
        }
}

#pragma mark - 接待完成事件
- (IBAction)receptionDoneAction:(id)sender {
    [self stopAudio];
    self.recordTimeLabel.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
    [[WTAudioObject shareInstance] stopAudio];
}

@end
